
//connect NodeJS 连接MongoDB 数据

// var mongoose = require('mongoose');

// const hostname = '0.0.0.0';
// const port = 27017;
// const user = 'root';
// const password = 'root';
// const dbname = 'wh2008';

// const db_conn_str = `mongodb://${hostname}:${port}/${dbname}`

// mongoose.connect(db_conn_str,function(err){
//     if(err){
//         console.log(err);
//         console.log('数据库连接失败！');
//     }else{
//         console.log('数据库连接成功！');
//     }
// })

// const connection = mongoose.connection

// connection.on("connected",function(){
//     console.log("数据库已经正确链接---connected")
// })

// connection.on("error",function(err){
//     console.log("数据库无法正常链接---error")
// })

// connection.on("disconnected",()=>{
//     console.log("数据库已经断开链接---disconnected")
// })

// module.exports = connection;



var mysql= require("mysql");
var pool = mysql.createPool({
    host: '8.136.10.30',
    user: 'root',
    password: 'root',
    database: 'school',
    port: 3306,
    timezone:"08:00"
});

//  执行多条查询语句
// var connection =  mysql.createConnection( { multipleStatements: true } );


var query=function(sql,callback){
    pool.getConnection(function(err,conn){
        if(err){
            callback(err,null,null);
        }else{
            conn.query(sql,function(qerr,vals,fields){
                //释放连接
                conn.release();
                //事件驱动回调
                callback(qerr,vals,fields);
            });
        }
    });
};

module.exports=query;

